﻿#ifndef HSFFMPEG_H
#define HSFFMPEG_H
#include "config/config.h"
#include <QThread>
#include <QImage>
#include <QPixmap>
#include <QObject>
#include <QWidget>
#include <QLabel>
#include <QPalette>
#include <QTimer>

const enum AVHWDeviceType HS_HardDecoder_Type=AV_HWDEVICE_TYPE_VAAPI;

class HS_PlayControl : public QTimer
{
    Q_OBJECT
public:
    HS_PlayControl()
    {
    }
    ~HS_PlayControl()
    {
    }
public slots:
    void HS_PlayControl_PlayStatusChange(int status){
        PRTLOG()<<"HS_PlayControl_PlayStatusChange"<<status;
        if(status == HSPlayer_Play){
            this->start();
        }else{
            this->stop();
        }
    }
    void HS_PlayControl_PlaySpeedChange(int frame,int num,int dec){
        this->stop();
        this->setInterval(1000/frame*dec/num);
        this->start();
    }
};

class HSFFmpeg : public QThread
{
    Q_OBJECT
public:
    void run();
    HSFFmpeg();
    ~HSFFmpeg();
    void HS_FFmpeg_SetUrl(StreamType stype,QString url);
    void HS_FFmpeg_Set_WindowRect(int x,int y,int w,int h);
    bool HS_FFmpeg_Init();
    bool HS_SDL2_Init();
    void HS_SDL2_Deinit();

    void HS_FFmpeg_ReSws();
    int  HS_FFmpeg_Get_FrameRate(){return this->HS_Video_FrameRate;}

private slots:
    void HS_FFmpeg_Play();
public:
    HS_PlayControl*  HS_Control=nullptr;
    Uint32            HS_Video_FrameRate=0;
private:
    QString           HS_StreamUrl;
    StreamType        HS_StreamType;
    HSPlayer_WindowRect HS_WindowRect;

    int               HS_Video_Width=-1;
    int               HS_Video_Height=-1;
    int               HS_Video_Index=-1;
    AVFormatContext*  HS_Video_FormatCtx=nullptr;
    AVCodecContext*   HS_Video_CodecCtx=nullptr;
    AVCodec*          HS_Video_Codec= nullptr;
    SwsContext*       HS_Video_SwsCtx= nullptr;
    AVBufferRef*      HS_Video_HWDevice_ctx = nullptr;
    const AVCodecHWConfig *   HS_Video_HWCodecConfig=nullptr;

    AVStream *        HS_Video_Stream;
    AVPicture         HS_Video_Picture;

    unsigned char*   HS_YUV_Data;
    SDL_Window*      HS_SDL2_VideoWidget=nullptr;
    SDL_Renderer*    HS_SDL2_VideoRender=nullptr;
    SDL_Texture*     HS_SDL2_VideoTexture=nullptr;
    SDL_Rect         HS_SDL2_VideoRect;
    SDL_Event        HS_SDL2_VideoEvent;
    QLabel*          HS_QT_VideoLabel;


private:
    int  HS_FFmpeg_DecodeVideo_Play(AVCodecContext *avctx, AVPacket *packet);
};
//rtsp://%s:554/cam/realmonitor?channel=0&subtype=0&unicast=true&proto=Onvif
#endif // HSFFMPEG_H
